<?php
	include_once("cprotect.php");
	
	class CQuery{
		private $table;
		
		////-------SELECT-------//
		public function selectGa($id = 'all'){			
			$this->table = "gallery";
			$protect = new CProtect();
			$query = 'SELECT *
					FROM '.$this->table;
			if($id != 'all'){
				$query .= ' WHERE idnews = '.$protect->getSQLValue($id, 'text');
			}			
			$query .= ' ORDER BY id ASC;';
			$result = mysql_query($query);
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
		
//-------RSS-------//		
				public function selectToRSS($amount = 1){
			$this->table = "platillos";
			$protect = new CProtect();
			$query = 'SELECT id, name, description, image, type, date
					FROM '.$this->table.			
					'WHERE type = 1';
					' ORDER BY date DESC, id DESC
					LIMIT '.$protect->getSQLValue($amount, 'int').';';
			$result = mysql_query($query);
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
		
//-------LAST ID-------//
		public function lastInsertedId(){
			$query = "SELECT LAST_INSERT_ID() as id;";
			$result = mysql_query($query);
			
			return $result;
		}
		
//-------PUBLICATIONS-------//
		////-------SELECT-------//
		public function selectPublications($id = 'all'){			
			$this->table = "news";
			$protect = new CProtect();
			$query = 'SELECT id, title_es, title_en, text_es, text_en, image, date, type
					FROM '.$this->table;
			if($id != 'all'){
				$query .= ' WHERE id = '.$protect->getSQLValue($id, 'int');
			}
			$query .= ' ORDER BY id DESC;';
			$result = mysql_query($query);
			
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
		
//----------------------------------Platillos ----------------------------------------//
		////-------SELECT-------//
		public function selectPlatilloSemanal($id = 'all'){			
			$this->table = "platillos";
			$protect = new CProtect();
			$query = 'SELECT id, name, description, image, type, position
					FROM '.$this->table.'
					WHERE type = 1';
			if($id != 'all'){
				$query .= ' AND id = '.$protect->getSQLValue($id, 'int');
			}
			$query .= ' ORDER BY id DESC;';
			$result = mysql_query($query);
			
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
		

		////-------SELECT-------//
		public function selectCasas($id = 'all'){			
			$this->table = "inmuebles";
			$protect = new CProtect();
			$query = 'SELECT * FROM inmuebles WHERE type = 1';
			if($id != 'all'){
				$query .= ' AND id = '.$protect->getSQLValue($id, 'int');
			}
			$query .= ' ORDER BY id DESC;';
			$result = mysql_query($query);
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
		//TERRENOS//////////////
				public function selectTerrenos($id = 'all'){			
			$this->table = "inmuebles";
			$protect = new CProtect();
			$query = 'SELECT * FROM inmuebles WHERE type = 2';
			if($id != 'all'){
				$query .= ' AND id = '.$protect->getSQLValue($id, 'int');
			}
			$query .= ' ORDER BY id DESC;';
			$result = mysql_query($query);
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
				public function selectFotos(){			
			$this->table = "images";
			$protect = new CProtect();
			$query = 'SELECT * FROM images';
			$query .= ' ORDER BY id DESC;';
			$result = mysql_query($query);
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
		
						public function selectFotos2($clave){			
			$this->table = "images";
			$protect = new CProtect();
			$query = 'SELECT * FROM images WHERE clave ='.$clave.';';
			$query .= ' ORDER BY id DESC;';
			$result = mysql_query($query);
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
		
					public function selectMarks(){			
			$this->table = "map";
			$protect = new CProtect();
			$query = 'SELECT * FROM map';
			$query .= ' ORDER BY id DESC;';
			$result = mysql_query($query);
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
		
						public function selectFotos1(){			
			$this->table = "inmuebles";
			$protect = new CProtect();
			$query = 'SELECT * FROM inmuebles WHERE type = 1';
			$query .= ' ORDER BY id DESC;';
			$result = mysql_query($query);
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
		
								//-------DELETE Marks-------//
		public function deleteMarks($clave){
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$this->table = "map";
			$query = 'DELETE FROM '.$this->table.'
					WHERE clave = '.$protect->getSQLValue($clave, 'text').';';
			$result = @mysql_query($query);
			if(mysql_affected_rows() < 1){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				mysql_query("COMMIT;");
				return true;
			}
		}
		
								//-------DELETE TERRENOS-------//
		public function deleteTerrenos($id){
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$this->table = "inmuebles";
			$query = 'DELETE FROM '.$this->table.'
					WHERE id = '.$protect->getSQLValue($id, 'int').';';
			$result = @mysql_query($query);
			if(mysql_affected_rows() < 1){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				mysql_query("COMMIT;");
				return true;
			}
		}
		
		
		                 ///-------INSERT CASAS-------//
				public function insertCasas($clave, $type, $image, $descripcion, $direccion, $precio, $colonia, $localidad){
				
			$this->table = "inmuebles";
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$query = 'INSERT INTO '.$this->table.'
					VALUES (null,
							'.$protect->getSQLValue(strip_tags($clave),'text').',
							'.$protect->getSQLValue(strip_tags($type),'text').',
							'.$protect->getSQLValue(strip_tags($image),'text').',
							'.$protect->getSQLValue($descripcion,'text').',
							'.$protect->getSQLValue(strip_tags($direccion),'text').',
							'.$protect->getSQLValue(strip_tags($precio),'text').',
							'.$protect->getSQLValue(strip_tags($colonia),'text').',
							'.$protect->getSQLValue(strip_tags($localidad),'text').');';
		    //strip_tags($title_es)
			$result = @mysql_query($query);
			if(mysql_affected_rows() < 1){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				mysql_query("COMMIT;");
				return true;
			}
		}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////		
		////-------insertNews-------//
						public function insertNews($titulo, $news, $username, $fuente, $categoria, $date, $time, $status, $image){
			$this->table = "news";
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$query = 'INSERT INTO '.$this->table.'
					VALUES (null,
							'.$protect->getSQLValue(strip_tags($titulo),'text').',
							'.$protect->getSQLValue($news,'text').',
							'.$protect->getSQLValue(strip_tags($username),'text').',
							'.$protect->getSQLValue(strip_tags($fuente),'text').',
							'.$protect->getSQLValue(strip_tags($categoria),'text').',
							NOW(),
							'.$protect->getSQLValue(strip_tags($time),'text').',
							'.$protect->getSQLValue(strip_tags($status),'text').',
							'.$protect->getSQLValue(strip_tags($image),'text').');';
		    //strip_tags($title_es)
			$result = @mysql_query($query);
			if(mysql_affected_rows() < 1){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				mysql_query("COMMIT;");
				return true;
			}
		}
		
		////-------insertNews-------//
						public function insertGallery($titulo, $news, $username, $fuente, $categoria, $date, $time, $status, $image){
			$this->table = "news";
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$query = 'INSERT INTO '.$this->table.'
					VALUES (null,
							'.$protect->getSQLValue(strip_tags($titulo),'text').',
							'.$protect->getSQLValue($news,'text').',
							'.$protect->getSQLValue(strip_tags($username),'text').',
							'.$protect->getSQLValue(strip_tags($fuente),'text').',
							'.$protect->getSQLValue(strip_tags($categoria),'text').',
							NOW(),
							'.$protect->getSQLValue(strip_tags($time),'text').',
							'.$protect->getSQLValue(strip_tags($status),'text').',
							'.$protect->getSQLValue(strip_tags($image),'text').');';
		    //strip_tags($title_es)
			$result = @mysql_query($query);
			if(mysql_affected_rows() < 1){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				mysql_query("COMMIT;");
				return true;
			}
		}
		
		

		////-------insertVideo-------//
						public function insertVideo($titulo, $news, $username, $fuente, $categoria, $date, $time, $status, $image){
			$this->table = "videos";
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$query = 'INSERT INTO '.$this->table.'
					VALUES (null,
							'.$protect->getSQLValue(strip_tags($titulo),'text').',
							'.$protect->getSQLValue($news,'text').',
							'.$protect->getSQLValue(strip_tags($username),'text').',
							'.$protect->getSQLValue(strip_tags($fuente),'text').',
							'.$protect->getSQLValue(strip_tags($categoria),'text').',
							NOW(),
							'.$protect->getSQLValue(strip_tags($time),'text').',
							'.$protect->getSQLValue(strip_tags($status),'text').',
							'.$protect->getSQLValue(strip_tags($image),'text').');';
		    //strip_tags($title_es)
			$result = @mysql_query($query);
			if(mysql_affected_rows() < 1){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				mysql_query("COMMIT;");
				return true;
			}
		}
		
		////-------SelectNews-------//
		public function selectNews($id = 'all'){			
			$this->table = "news";
			$protect = new CProtect();
			$query = 'SELECT *
					FROM '.$this->table.'';					
			if($id != 'all'){
				$query .= ' WHERE id = '.$protect->getSQLValue($id, 'int');
			}
			$query .= ' ORDER BY id DESC;';
			$result = mysql_query($query);
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
		
		////-------SelectNewsByUser Serch-------//
		public function selectNewsSerch($id = 'all'){			
			$this->table = "news";
			$protect = new CProtect();
			$query = 'SELECT *
					FROM '.$this->table.'';					
			if($id != 'all'){
				$query .= ' WHERE username = '.$protect->getSQLValue($id, 'text');
			}
			$query .= ' ORDER BY id DESC;';
			$result = mysql_query($query);
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
	
		////-------SelectNews-------//
		public function selectColumn($id = 'all'){			
			$this->table = "opinion";
			$protect = new CProtect();
			$query = 'SELECT *
					FROM '.$this->table.'';
					
			if($id != 'all'){
				$query .= ' WHERE id = '.$protect->getSQLValue($id, 'int');
			}
			$query .= ' ORDER BY id DESC;';
			$result = mysql_query($query);
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}

		////-------SelectNews-------//
		public function selectVideos($id = 'all'){			
			$this->table = "videos";
			$protect = new CProtect();
			$query = 'SELECT *
					FROM '.$this->table.'';
					
			if($id != 'all'){
				$query .= ' WHERE id = '.$protect->getSQLValue($id, 'int');
			}
			$query .= ' ORDER BY id DESC;';
			$result = mysql_query($query);
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
		
		////-------SelectUserNews-------//
		public function selectUserNews($id = 'all', $last ='no'){			
			$this->table = "news";
			$protect = new CProtect();
			$query = 'SELECT *
					FROM '.$this->table.'';
			if($id != 'all'){
				$query .= ' WHERE username = '.$protect->getSQLValue($id, 'text');
			}
			if($last == 'yes'){
			    $query .= ' ORDER BY id DESC;';
			}else {$query .= ' ORDER BY id DESC;';
					}
			$result = mysql_query($query);
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
				////-------SelectNews-------//
		public function selectHotNews($id = 'all'){			
			$this->table = "news";
			$protect = new CProtect();
			$query = 'SELECT *
					FROM '.$this->table.' WHERE status = 3';					
			if($id != 'all'){
				$query .= ' AND id = '.$protect->getSQLValue($id, 'int');
			}
			$query .= ' ORDER BY date DESC;';
			$result = mysql_query($query);
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
						////-------SelectNews-------//
		public function selectTopHotNews($id = 'all'){			
			$this->table = "news";
			$protect = new CProtect();
			$query = 'SELECT *
					FROM '.$this->table.' WHERE status = 4';					
			if($id != 'all'){
				$query .= ' AND id = '.$protect->getSQLValue($id, 'int');
			}
			$query .= ' ORDER BY date DESC;';
			$result = mysql_query($query);
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
		
		
		////-------SelectNews-------//
		public function selectNewsCat($id = 'all'){			
			$this->table = "news";
			$protect = new CProtect();
			$query = 'SELECT *
					FROM '.$this->table.'';
					
			if($id != 'all'){
				$query .= ' WHERE categoria = '.$protect->getSQLValue($id, 'int');
			}
			$query .= ' ORDER BY id DESC;';
			$result = mysql_query($query);
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
		
		
		////-------SelectSomeNews-------//
		public function selectSomeNews($id = 'all'){			
			$this->table = "news";
			$protect = new CProtect();
			$query = 'SELECT *
					FROM '.$this->table.'';
					
			if($id != 'all'){
				$query .= ' AND id = '.$protect->getSQLValue($id, 'int');
			}
			$query .= ' ORDER BY date DESC;';
			$result = mysql_query($query);
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}

		////-------SelectCatNews-------//
		public function selectCatNews($id = 'all'){			
			$this->table = "news";
			$protect = new CProtect();
			$query = 'SELECT *
					FROM '.$this->table.'';
					
			if($id != 'all'){
				$query .= ' WHERE categoria = '.$protect->getSQLValue($id, 'text');
			}
			$query .= ' ORDER BY date DESC;';
			$result = mysql_query($query);
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
		
		////-------insertNews-------//
		public function insertClient($clave, $username, $nombre, $mail, $password, $cuenta, $fecha, $pin, $whatsapp, $imessage, $sms, $correo, $suscripciones){
			$this->table = "clients";
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$query = 'INSERT INTO '.$this->table.'
					VALUES (null,
							'.$protect->getSQLValue(strip_tags($clave),'text').',
							'.$protect->getSQLValue(strip_tags($username),'text').',
							'.$protect->getSQLValue(strip_tags($nombre),'text').',
							'.$protect->getSQLValue(strip_tags($mail),'text').',
							'.$protect->getSQLValue(strip_tags($password),'text').',
							'.$protect->getSQLValue(strip_tags($cuenta),'text').',
							'.$protect->getSQLValue(strip_tags($fecha),'text').',
							'.$protect->getSQLValue(strip_tags($pin),'text').',
							'.$protect->getSQLValue(strip_tags($whatsapp),'text').',
							'.$protect->getSQLValue(strip_tags($imessage),'text').',
							'.$protect->getSQLValue(strip_tags($sms),'text').',
							'.$protect->getSQLValue(strip_tags($correo),'text').',
							'.$protect->getSQLValue(strip_tags($suscripciones),'text').');';
		    //strip_tags($title_es)
$result = @mysql_query($query);
			if(mysql_affected_rows() < 1){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				mysql_query("COMMIT;");
				return true;
			}
		}
	
//-------UPDATE VIDEOS-------//
		public function updateVideos($id, $titulo, $news, $fuente, $image){
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$this->table = "videos";
			$query = 'UPDATE '.$this->table.'
					SET titulo = '.$protect->getSQLValue($titulo,'text').', 
						news = '.$protect->getSQLValue($news,'text').', 
						fuente = '.$protect->getSQLValue($fuente,'text').',
						link = '.$protect->getSQLValue($image,'text').'
					WHERE id = '.$protect->getSQLValue($id, 'int').';
					';
			$result = @mysql_query($query);
			if(mysql_affected_rows() === false){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				mysql_query("COMMIT");
				return true;
			}
		}
		
			//-------UPDATE GALERIA-------//
		public function updateGaleria($idnews, $date, $status, $image, $name){
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$this->table = "gallery";
			$query = 'UPDATE '.$this->table.'
					SET date = '.$protect->getSQLValue($date,'text').', 
						status = '.$protect->getSQLValue($status,'text').', 
					'.$image.' = '.$protect->getSQLValue($name,'text').'
					WHERE idnews = '.$protect->getSQLValue($idnews, 'int').';';
			$result = @mysql_query($query);
			if(mysql_affected_rows() === 0){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				mysql_query("COMMIT");
				return true;
			}
		}

	//-------UPDATE GALERIA-------//
		public function updateGallerybyname($num,$id){
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$this->table = "gallery";
			$query = 'UPDATE '.$this->table.'
					SET image'.$num.' = '.$protect->getSQLValue("",'text').' 
					WHERE id = '.$protect->getSQLValue($id, 'int').';';
			$result = @mysql_query($query);
			if(mysql_affected_rows() === 0){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				mysql_query("COMMIT");
				return true;
			}
		}
		
		
///-------------------------Reportajes------------------------///
		public function selectReportajes($id = 'all'){			
			$this->table = "reportajes";
			$protect = new CProtect();
			$query = 'SELECT * FROM '.$this->table.'';
			if($id != 'all'){
				$query .= ' WHERE id = '.$protect->getSQLValue($id, 'int');
			}
			$query .= ' ORDER BY id DESC;';
			$result = mysql_query($query);
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
///-------------------------Reportajes------------------------///

///------------- COLUMNISTAS Y DEMAS -------------///
		public function selectColumnists($id = 'all'){			
			$this->table = "columnists";
			$protect = new CProtect();
			$query = 'SELECT * FROM '.$this->table.'';
			if($id != 'all'){
				$query .= ' WHERE id = '.$protect->getSQLValue($id, 'int');
			}
			$query .= ' ORDER BY id;';
			$result = mysql_query($query);
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
		
		public function selectOpinion($id = 'all'){			
			$this->table = "opinion";
			$protect = new CProtect();
			$query = 'SELECT * FROM '.$this->table.'';
			if($id != 'all'){
				$query .= ' WHERE idColumnist = '.$protect->getSQLValue($id, 'int');
			}
			$query .= ' ORDER BY id;';
			$result = mysql_query($query);
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}

		
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////		
		public function insertMarkshhh($clave, $lat, $lng, $colonia, $localidad){
				
			$this->table = "map";
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$query = 'INSERT INTO '.$this->table.'
					VALUES (null,
							'.$protect->getSQLValue(strip_tags($clave),'text').',
							'.$protect->getSQLValue(strip_tags($colonia),'text').',
							'.$protect->getSQLValue(strip_tags($lat),'text').',
							'.$protect->getSQLValue(strip_tags($lng),'text').',
							'.$protect->getSQLValue(strip_tags($localidad),'text').');';
		    //strip_tags($title_es)
			$result = @mysql_query($query);
			if(mysql_affected_rows() < 1){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				mysql_query("COMMIT;");
				return true;
			}
		}
		
				public function insertGaleria($idnews, $date, $status, $image1,$image2,$image3,$image4,$image5,$image6,$image7,$image8,$image9,$image10,$image11,$image12,$image13,$image14,$image15,$image16,$image17,$image18,$image19,$image20){
				
			$this->table = "gallery";
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$query = 'INSERT INTO '.$this->table.'
					VALUES (NULL , '.$protect->getSQLValue(strip_tags($idnews),'int').',
							'.$protect->getSQLValue(strip_tags($date),'text').',
							'.$protect->getSQLValue(strip_tags($status),'int').',
							'.$protect->getSQLValue(strip_tags($image1),'text').',
							'.$protect->getSQLValue(strip_tags($image2),'text').',
							'.$protect->getSQLValue(strip_tags($image3),'text').',
							'.$protect->getSQLValue(strip_tags($image4),'text').',
							'.$protect->getSQLValue(strip_tags($image5),'text').',
							'.$protect->getSQLValue(strip_tags($image6),'text').',
							'.$protect->getSQLValue(strip_tags($image7),'text').',
							'.$protect->getSQLValue(strip_tags($image8),'text').',
							'.$protect->getSQLValue(strip_tags($image9),'text').',
							'.$protect->getSQLValue(strip_tags($image10),'text').',
							'.$protect->getSQLValue(strip_tags($image11),'text').',
							'.$protect->getSQLValue(strip_tags($image12),'text').',
							'.$protect->getSQLValue(strip_tags($image13),'text').',
							'.$protect->getSQLValue(strip_tags($image14),'text').',
							'.$protect->getSQLValue(strip_tags($image15),'text').',
							'.$protect->getSQLValue(strip_tags($image16),'text').',
							'.$protect->getSQLValue(strip_tags($image17),'text').',
							'.$protect->getSQLValue(strip_tags($image18),'text').',
							'.$protect->getSQLValue(strip_tags($image19),'text').',
							'.$protect->getSQLValue(strip_tags($image20),'text').');';
			//strip_tags($title_es)
			$result = @mysql_query($query);
			if(mysql_affected_rows() < 1){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				mysql_query("COMMIT;");
				return true;
			}
		}
		
		
		
	
		
				//-------UPDATE CASAS-------//
		public function updateNews($id, $titulo, $news, $fuente, $categoria, $status, $image){
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$this->table = "news";
			$query = 'UPDATE '.$this->table.'
					SET titulo = '.$protect->getSQLValue($titulo,'text').', 
						news = '.$protect->getSQLValue($news,'text').', 
						fuente = '.$protect->getSQLValue($fuente,'text').',
						categoria = '.$protect->getSQLValue($categoria,'text').',
						status = '.$protect->getSQLValue($status,'text').',
						image = '.$protect->getSQLValue($image,'text').'
					WHERE id = '.$protect->getSQLValue($id, 'int').';
					';
			$result = @mysql_query($query);
			if(mysql_affected_rows() === false){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				mysql_query("COMMIT");
				return true;
			}
		}
		
						//-------UPDATE CASAS-------//
		public function updateCasasImg($id, $clave, $type, $image, $descripcion, $direccion, $precio, $colonia, $localidad){
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$this->table = "inmuebles";
			$query = 'UPDATE '.$this->table.'
					SET clave = '.$protect->getSQLValue($clave,'text').', 
						type = '.$protect->getSQLValue($type,'text').', 
						image = '.$protect->getSQLValue($image,'text').', 
						descripcion = '.$protect->getSQLValue($descripcion,'text').',
						direccion = '.$protect->getSQLValue($direccion,'text').',
						precio = '.$protect->getSQLValue($precio,'text').',
						colonia = '.$protect->getSQLValue($colonia,'text').',
						localidad = '.$protect->getSQLValue($localidad,'text').' 
					WHERE id = '.$protect->getSQLValue($id, 'int').';
					';
			$result = @mysql_query($query);
			if(mysql_affected_rows() === false){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				mysql_query("COMMIT");
				return true;
			}
		}
		
						
		
				//-------DELETE-------//
		public function deletePlatillos($id){
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$this->table = "platillos";
			$query = 'DELETE FROM '.$this->table.'
					WHERE id = '.$protect->getSQLValue($id, 'int').';';
			$result = @mysql_query($query);
			if(mysql_affected_rows() < 1){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				mysql_query("COMMIT;");
				return true;
			}
		}
		
		
//----------------------------------Platillo Semanal-----------------------------------------//		

		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		public function selectLastid($amount = 1){
			$this->table = "inmuebles";
			$protect = new CProtect();
			$query = 'SELECT id, title_es, title_en, text_es, text_en, image, date
					FROM '.$this->table.			
					' WHERE type = 1
					ORDER BY date DESC, id DESC
					LIMIT '.$protect->getSQLValue($amount, 'int').';';
			$result = mysql_query($query);
			
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
		

		


		
		//-------INSERT-------//
		public function insertNewsss($title_es, $title_en, $text_es, $text_en, $image, $date, $type = 1, $position = 0){
			$this->table = "news";
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$query = 'INSERT INTO '.$this->table.'
					VALUES (0,
							'.$protect->getSQLValue($title_es,'text').',
							'.$protect->getSQLValue($title_en,'text').',
							'.$protect->getSQLValue($text_es,'text').',
							'.$protect->getSQLValue($text_en,'text').',
							'.$protect->getSQLValue($image,'text').',
							NOW(),
							'.$protect->getSQLValue($type,'int').',
							 null
							);';
			
			$result = @mysql_query($query);
			if(mysql_affected_rows() < 1){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				mysql_query("COMMIT;");
				return true;
			}
		}
		
		//-------DELETE-------//
		public function deleteNews($id){
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$this->table = "news";
			$query = 'DELETE FROM '.$this->table.'
					WHERE id = '.$protect->getSQLValue($id, 'int').';';
			$result = @mysql_query($query);
			if(mysql_affected_rows() < 1){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				mysql_query("COMMIT;");
				return true;
			}
		}
			//-------DELETE-------//
		public function deleteVideos($id){
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$this->table = "videos";
			$query = 'DELETE FROM '.$this->table.'
					WHERE id = '.$protect->getSQLValue($id, 'int').';';
			$result = @mysql_query($query);
			if(mysql_affected_rows() < 1){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				mysql_query("COMMIT;");
				return true;
			}
		}

	//-------DELETE-------//
		public function deleteGallery($id){
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$this->table = "gallery";
			$query = 'DELETE FROM '.$this->table.'
					WHERE id = '.$protect->getSQLValue($id, 'text').';';

			$result = @mysql_query($query);
			if(mysql_affected_rows() < 1){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				mysql_query("COMMIT;");
				return true;
			}
		}
		
//-------PROMOTIONS-------//
		////-------SELECT-------//
		public function selectPromotions($id = 'all'){			
			$this->table = "news";
			$protect = new CProtect();
			$query = 'SELECT id, title_es, title_en, text_es, text_en, image, date, position
					FROM '.$this->table.'
					WHERE type = 2';
			if($id != 'all'){
				$query .= ' AND id = '.$protect->getSQLValue($id, 'int');
			}
			$query .= ' ORDER BY id DESC;';
			$result = mysql_query($query);
			
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
		
		public function selectPromotionsByPosition(){			
			$this->table = "news";
			$protect = new CProtect();
			$query = 'SELECT id, title_es, title_en, text_es, text_en, image, date, position
					FROM '.$this->table.'
					WHERE type = 2
					ORDER BY position ASC;';
			$result = mysql_query($query);
			
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
		
		
		public function selectLastPromotions($amount = 1){
			$this->table = "news";
			$protect = new CProtect();
			$query = 'SELECT id, title_es, title_en, text_es, text_en, image, date, position 
			          FROM '.$this->table.			
					' WHERE type = 2
					ORDER BY date DESC, id DESC
					LIMIT '.$protect->getSQLValue($amount, 'int').';';
			$result = mysql_query($query);
			
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
	
		//-------INSERT-------//
		public function insertPromotions($title_es, $title_en, $text_es, $text_en, $image, $date, $type = 2){
			$this->table = "news";
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$query = 'INSERT INTO '.$this->table.'
					VALUES (0,
							'.$protect->getSQLValue($title_es,'text').',
							'.$protect->getSQLValue($title_en,'text').',
							'.$protect->getSQLValue($text_es,'text').',
							'.$protect->getSQLValue($text_en,'text').',
							'.$protect->getSQLValue($image,'text').',
							NOW(),
							'.$protect->getSQLValue($type,'int').',
							0
							);';
			$result = @mysql_query($query);
			if(mysql_affected_rows() < 1){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				mysql_query("COMMIT;");
				return true;
			}
		}
		
		//-------UPDATE INDEX-------//
		public function updatePromotionsIndex($id, $newIndex, $oldIndex = 0){
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$this->table = "news";
			
			if($newIndex == -1){
				$query = 'UPDATE '.$this->table.'
						SET position = position - 1
						WHERE position > '.$protect->getSQLValue($oldIndex, 'int').'
						;';
			}else if($oldIndex == 0){
				$query = 'UPDATE '.$this->table.'
						SET position = position + 1
						WHERE position >= '.$protect->getSQLValue($newIndex, 'int').'
						;';
			}else{
				if($newIndex > $oldIndex){
					$query = 'UPDATE '.$this->table.'
							SET position = position - 1
							WHERE position > '.$protect->getSQLValue($oldIndex, 'int').'
								AND position <= '.$protect->getSQLValue($newIndex, 'int').'
							;';
				}else{
					$query = 'UPDATE '.$this->table.'
							SET position = position + 1
							WHERE position < '.$protect->getSQLValue($oldIndex, 'int').'
								AND position >= '.$protect->getSQLValue($newIndex, 'int').'
							;';
				}
			}

			$result = @mysql_query($query);
			if(mysql_affected_rows() === false){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				if($newIndex == -1){
					mysql_query("COMMIT");
					return true;
				}
				$query = 'UPDATE '.$this->table.'
						SET position = '.$protect->getSQLValue($newIndex, 'int').'
						WHERE id = '.$protect->getSQLValue($id, 'int').'
						;';
				$result = @mysql_query($query);
				if(mysql_affected_rows() === false){
					mysql_query("ROLLBACK;");
					return false;
				}else{
					mysql_query("COMMIT");
					return true;
				}
			}
		}
		
//-------GALLERIES-------//
		////-------SELECT-------//
		public function selectGallery($id = 'all'){			
			$this->table = "gallery";
			$protect = new CProtect();
			$query = 'SELECT *
					FROM '.$this->table;
			if($id != 'all'){
				$query .= ' WHERE idnews = '.$protect->getSQLValue($id, 'int');
			}
			$query .= ' ORDER BY id ASC;';
			$result = mysql_query($query);

			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}

		////-------SELECT-------//
		public function selectGalleryy($id = 'all'){			
			$this->table = "gallery";
			$protect = new CProtect();
			$query = 'SELECT *
					FROM '.$this->table;
			if($id != 'all'){
				$query .= ' WHERE idNews = '.$protect->getSQLValue($id, 'text');
			}
			$query .= ' ORDER BY id DESC;';
			$result = mysql_query($query);

			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}

		
		////-------SELECT-------//
		public function selectGalleryNews($id = 'all'){			
			$this->table = "news";
			$protect = new CProtect();
			$query = 'SELECT *
					FROM '.$this->table;
			if($id != 'all'){
				$query .= ' WHERE idnews = '.$protect->getSQLValue($id, 'int');
			}
			$query .= ' ORDER BY id DESC;';
			$result = mysql_query($query);
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
		
//-------IMAGES-------//
		////-------SELECT-------//
		public function selectImage($id){			
			$this->table = "images";
			$protect = new CProtect();
			$query = 'SELECT '.$this->table.'.*, gallery.name as galleryName, gallery.id as galleryId
					FROM '.$this->table.', galleries as gallery
					WHERE ('.$this->table.'.id_gallery = gallery.id)
					AND '.$this->table.'.id = '.$protect->getSQLValue($id, 'int').';';
			
			$result = mysql_query($query);
			
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
		
		public function selectImagesByGallery($galeryId){			
			$this->table = "images";
			$protect = new CProtect();
			$query = 'SELECT '.$this->table.'.*, gallery.name as galleryName
					FROM '.$this->table.', galleries as gallery
					WHERE ('.$this->table.'.id_gallery = gallery.id)
					AND gallery.id = '.$protect->getSQLValue($galeryId, 'int').';';
			
			$result = mysql_query($query);
			
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
		
		//-------UPDATE-------//
		public function updateImage($id, $title_es, $title_en, $text_es, $text_en, $image, $gallery){
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$this->table = "images";
			$query = 'UPDATE '.$this->table.'
					SET id_gallery = '.$protect->getSQLValue($gallery,'int').', 
						title_es = '.$protect->getSQLValue($title_es,'text').', 
						title_en = '.$protect->getSQLValue($title_en,'text').', 
						text_es = '.$protect->getSQLValue($text_es,'text').', 
						text_en = '.$protect->getSQLValue($text_en,'text').', 
						image = '.$protect->getSQLValue($image,'text').'
					WHERE id = '.$protect->getSQLValue($id, 'int').';
					';
			$result = @mysql_query($query);
			if(mysql_affected_rows() === false){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				mysql_query("COMMIT");
				return true;
			}
		}
		
		////-------INSERT-------//
		public function insertImage($title_es, $title_en, $text_es, $text_en, $image, $gallery){
			$this->table = "images";
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$query = 'INSERT INTO '.$this->table.'
					VALUES (0,
							'.$protect->getSQLValue($gallery,'int').',
							'.$protect->getSQLValue($title_es,'text').',
							'.$protect->getSQLValue($title_en,'text').',
							'.$protect->getSQLValue($text_es,'text').',
							'.$protect->getSQLValue($text_en,'text').',
							'.$protect->getSQLValue($image,'text').'
							);';
			$result = @mysql_query($query);
			if(mysql_affected_rows() < 1){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				mysql_query("COMMIT;");
				return true;
			}
		}
		
		//-------DELETE-------//
		public function deleteImage($id){
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$this->table = "images";
			$query = 'DELETE FROM '.$this->table.'
					WHERE id = '.$protect->getSQLValue($id, 'int').';';
			$result = @mysql_query($query);
			if(mysql_affected_rows() < 1){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				mysql_query("COMMIT;");
				return true;
			}
		}
		
//-------Welcome-------//
		////-------SELECT-------//
		public function selectWelcome($id = 'all'){			
			$this->table = "welcome";
			$protect = new CProtect();
			$query = 'SELECT *
					FROM '.$this->table;
			if($id != 'all'){
				$query .= ' WHERE id = '.$protect->getSQLValue($id, 'int');
			}
			$query .= ' ORDER BY id DESC;';
			$result = mysql_query($query);
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
				
			public function selectWelcomePublications($id = 'all'){			
			$this->table = "welcome";
			$protect = new CProtect();
			$query = 'SELECT id, title_es, title_en, text_es, text_en, image, date,
					FROM '.$this->table;
			if($id != 'all'){
				$query .= ' WHERE id = '.$protect->getSQLValue($id, 'int');
			}
			$query .= ' ORDER BY id DESC;';
			$result = mysql_query($query);
			
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
					
				
			
		public function selectWelcomeByPosition(){			
			$this->table = "welcome";
			$protect = new CProtect();
			$query = 'SELECT id, title_es, title_en, text_es, text_en, image, date
					FROM '.$this->table.'
					ORDER BY position ASC;';
			$result = mysql_query($query);
			
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
		
				public function deleteWelcome($id){
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$this->table = "welcome";
			$query = 'DELETE FROM '.$this->table.'
					WHERE id = '.$protect->getSQLValue($id, 'int').';';
			$result = @mysql_query($query);
			if(mysql_affected_rows() < 1){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				mysql_query("COMMIT;");
				return true;
			}
		}
		
				public function updateWelcome($id, $title_es, $title_en, $text_es, $text_en, $image){
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$this->table = "welcome";
			$query = 'UPDATE '.$this->table.'
					SET title_es = '.$protect->getSQLValue($title_es,'text').', 
						title_en = '.$protect->getSQLValue($title_en,'text').', 
						text_es = '.$protect->getSQLValue($text_es,'text').', 
						text_en = '.$protect->getSQLValue($text_en,'text').', 
						image = '.$protect->getSQLValue($image,'text').'
					WHERE id = '.$protect->getSQLValue($id, 'int').';
					';
			$result = @mysql_query($query);
			if(mysql_affected_rows() === false){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				mysql_query("COMMIT");
				return true;
			}
		}
		
		
		
		public function selectLastWelcome($amount = 1){
			$this->table = "welcome";
			$protect = new CProtect();
			$query = 'SELECT id, title_es, title_en, text_es, text_en, image, date
					FROM '.$this->table.'';
					'ORDER BY date DESC, id DESC
					LIMIT '.$protect->getSQLValue($amount, 'int').';';
			$result = mysql_query($query);
			
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
/////----- Movies ----- /////
        
        public function insertMovie($movie,$class,$sala,$cine,$hora,$image){
			$this->table = "cartelera";
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$query = 'INSERT INTO '.$this->table.'
					VALUES (null,
							'.$protect->getSQLValue(strip_tags($movie),'text').',
							'.$protect->getSQLValue(strip_tags($class),'text').',
                            '.$protect->getSQLValue(strip_tags($hora),'text').',
							'.$protect->getSQLValue(strip_tags($sala),'text').',
							'.$protect->getSQLValue(strip_tags($cine),'text').',
							'.$protect->getSQLValue(strip_tags($image),'text').');';
		    //strip_tags($title_es)
			$result = @mysql_query($query);
			if(mysql_affected_rows() < 1){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				mysql_query("COMMIT;");
				return true;
			}
		}
        public function selectMovie($id = 'all'){			
			$this->table = "cartelera";
			$protect = new CProtect();
			$query = 'SELECT *
					FROM '.$this->table.'';					
			if($id != 'all'){
				$query .= ' WHERE id = '.$protect->getSQLValue($id, 'int');
			}
			$query .= ' ORDER BY id DESC;';
			$result = mysql_query($query);
			if(!mysql_num_rows($result)){
				return "error";
			}else{
				return $result;
			}
		}
        
        public function deleteMovie($id){
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$this->table = "cartelera";
			$query = 'DELETE FROM '.$this->table.'
					WHERE id = '.$protect->getSQLValue($id, 'int').';';
			$row = mysql_fetch_array($imagen);
            $result = @mysql_query($query);
			if(mysql_affected_rows() < 1){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				mysql_query("COMMIT;");
				return true;
			}
		}
        

	
		//-------INSERT-------//
				public function insertWelcome($title_es, $title_en, $text_es, $text_en, $image, $date){
			$this->table = "welcome";
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$query = 'INSERT INTO '.$this->table.'
					VALUES (0,
							'.$protect->getSQLValue($title_es,'text').',
							'.$protect->getSQLValue($title_en,'text').',
							'.$protect->getSQLValue($text_es,'text').',
							'.$protect->getSQLValue($text_en,'text').',
							'.$protect->getSQLValue($image,'text').',
							NOW());';
			//strip_tags($title_es)
			$result = @mysql_query($query);
			if(mysql_affected_rows() < 1){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				mysql_query("COMMIT;");
				return true;
			}
			
		}		
		
		
		//-------UPDATE INDEX-------//
		public function updateWelcomeIndex($id, $newIndex, $oldIndex = 0){
			mysql_query("BEGIN;");
			$protect = new CProtect();
			$this->table = "welcome";
			
			if($newIndex == -1){
				$query = 'UPDATE '.$this->table.'
						SET position = position - 1
						WHERE position > '.$protect->getSQLValue($oldIndex, 'int').'
						;';
			}else if($oldIndex == 0){
				$query = 'UPDATE '.$this->table.'
						SET position = position + 1
						WHERE position >= '.$protect->getSQLValue($newIndex, 'int').'
						;';
			}else{
				if($newIndex > $oldIndex){
					$query = 'UPDATE '.$this->table.'
							SET position = position - 1
							WHERE position > '.$protect->getSQLValue($oldIndex, 'int').'
								AND position <= '.$protect->getSQLValue($newIndex, 'int').'
							;';
				}else{
					$query = 'UPDATE '.$this->table.'
							SET position = position + 1
							WHERE position < '.$protect->getSQLValue($oldIndex, 'int').'
								AND position >= '.$protect->getSQLValue($newIndex, 'int').'
							;';
				}
			}

			$result = @mysql_query($query);
			if(mysql_affected_rows() === false){
				mysql_query("ROLLBACK;");
				return false;
			}else{
				if($newIndex == -1){
					mysql_query("COMMIT");
					return true;
				}
				$query = 'UPDATE '.$this->table.'
						SET position = '.$protect->getSQLValue($newIndex, 'int').'
						WHERE id = '.$protect->getSQLValue($id, 'int').'
						;';
				$result = @mysql_query($query);
				if(mysql_affected_rows() === false){
					mysql_query("ROLLBACK;");
					return false;
				}else{
					mysql_query("COMMIT");
					return true;
				}
			}
		}
		
		
		
	}
	
	
	
?>